package io.yunho.srw.lean.lambda;

import org.junit.Test;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.function.Supplier;

/**
 * Created by Administrator on 2019/12/16.
 */
public class Testlambda {
    /**
     * 消费型接口
     */
    @Test
    public void test1(){
        happy(2000,m->System.out.println("打车消费了"+m+"元"));
    }
    public void happy(double money, Consumer<Double> consumer){
        consumer.accept(money);
    }
    /**
     * 供给型接口
     */
    @Test
    public void test2(){
        sup(10, ()->(int)(Math.random()*100)).forEach(e->System.out.println(e));
    }
    public List<Integer> sup(int size, Supplier<Integer> supplier){
        List<Integer> list=new ArrayList<>();
        for (int i=0;i<size;i++){
            list.add(supplier.get());
        }
        return list;
    }
    /**
     * 函数型接口
     */
    @Test
    public void test3(){
        System.out.println(dealStr("我去你大2222爷",x->x.length()));
    }
    public Integer dealStr(String s, Function<String,Integer> function){
        return function.apply(s);
    }
    /**
     * 断言型接口
     */
    @Test
    public void test4(){
        List<String> list = Arrays.asList("李四","张三","王五");
        judgeInList(list,x->x.contains("李")).forEach(System.out::println);
    }
    public List<String> judgeInList(List<String> list,Predicate<String> predicate){
        List<String> list1=new ArrayList<>();
        list.forEach(s -> {
            if(predicate.test(s)){
                list1.add(s);
            }
        });
        return list1;
    }
}
